var tree;
var setting = {
	data : {
		simpleData : {
			enable : true,
			idKey : "id",
			pIdKey : "pid"
		},
		view : {
			dblClickExpand : false,// 双击节点图标展开节点
			selectedMulti : false
		}
	},
	callback : {
		onRightClick : function(event, treeId, treeNode) {//右键
			// 判断点击了tree的“空白”部分，即没有点击到tree节点上
			if (!treeNode && event.target.tagName.toLowerCase() != "button"
					&& $(event.target).parents("a").length == 0) {
				tree.cancelSelectedNode();
				$("#addFolder").removeClass("menu-item-disabled");
				$("#updateFolder").addClass("menu-item-disabled");
				$("#delFolder").addClass("menu-item-disabled");
				$("#add").addClass("menu-item-disabled");
				$("#update").addClass("menu-item-disabled");
				$("#del").addClass("menu-item-disabled");
			} else if (treeNode && !treeNode.noR) { // 判断点击的是tree节点
				tree.selectNode(treeNode); // 选中tree节点
				if (treeNode.parentTId) {
					//不是根节点
					$('#addFolder').addClass("menu-item-disabled");
					$("#updateFolder").addClass("menu-item-disabled");
					$("#delFolder").addClass("menu-item-disabled");
					$("#add").addClass("menu-item-disabled");
					$("#update").removeClass("menu-item-disabled");
					$("#del").removeClass("menu-item-disabled");
				} else {
					//是根节点
					$('#addFolder').addClass("menu-item-disabled");
					$("#updateFolder").removeClass("menu-item-disabled");
					$("#delFolder").removeClass("menu-item-disabled");
					$("#add").removeClass("menu-item-disabled");
					$("#update").addClass("menu-item-disabled");
					$("#del").addClass("menu-item-disabled");
				}
				/*if (treeNode.children && treeNode.children.length > 0) { // 这里是子节点
					$('#addFolder').addClass("menu-item-disabled");
					$("#add").removeClass("menu-item-disabled");
					$("#update").addClass("menu-item-disabled");
					$("#del").addClass("menu-item-disabled");
				}*/
			}
			$('#mm').menu({
				onClick : function(item) {
					if (item.name == "addFolder") {
						$("#foldermenu").show();
						$("#menu").hide();
						openDialog("新建根目录", "330", "380", "addFolder");
					}
					if (item.name == "updateFolder") {
						$("#foldermenu").show();
						$("#menu").hide();
						openDialog("修改根目录", "330", "380", "updateFolder");
						getUpdate(tree.getSelectedNodes()[0].id);
					}
					if (item.name == "delFolder") {
						delFolder();
					}
					if (item.name == "add") {
						$("#foldermenu").hide();
						$("#menu").show();
						openDialog("添加菜单", "330", "380", "add");
					}
					if (item.name == "update") {
						$("#foldermenu").hide();
						$("#menu").show();
						openDialog("修改菜单", "330", "380", "update");
						getUpdate(tree.getSelectedNodes()[0].id);
					}
					if (item.name == "del") {
						delAction();
					}
				}
			});
			$('#mm').menu('show', {
				left : event.pageX,
				top : event.pageY
			});
		}/*,
				onClick : function(event, treeId, treeNode) {//左键
		//			alert(treeNode.name + "," + treeNode.isParent + "," + "," + treeNode.id);
					alert("当前节点是" + treeNode.id);
				}*/
	}
};
var zNodes;

$(document).ready(function() {
	$('#sort,#folderSort').numberspinner({
		min : 1,
		max : 10000
	});
	$('#path').validatebox({
		required : true
	});
	$('#sort,#folderSort').numberspinner('setValue', 1);
	getZtreeData();
	tree = $.fn.zTree.init($("#tree"), setting, zNodes);
	$("#menuPanel").panel({
		width : 500,
		height : getWindowSize()[1] - 20,
		title : "菜单",
		tools : [ {
			iconCls : 'icon-add',
			handler : function() {
				$("#foldermenu").show();
				$("#menu").hide();
				openDialog("新建根目录", "330", "380", "addFolder");
			}
		} ]
	});
});
function getZtreeData() {
	$.ajax({
		type : "post",
		async : false,
		url : "/rbac/getMenuTree",
		dataType : "json",
		data:{
			'type':'menu'
		},
		success : function(result) {
			zNodes = result;
		}
	});
	tree = $.fn.zTree.init($("#tree"), setting, zNodes);
}
function getUpdate(id) {
	$.ajax({
		type : "post",
		async : false,
		url : "/rbac/getAction",
		data : {
			"resources.id" : id
		},
		dataType : "json",
		success : function(result) {
			$("#name,#folderName").val(result.msg.name);
			$("#path").val(result.msg.path);
			$('#sort,#folderSort').numberspinner('setValue', result.msg.sort);
		}
	});
}
function openDialog(title, width, height, name) {
	clearCache();
	$('#dialog').dialog({
		title : title,
		width : width,
		height : height,
		closed : false,
		cache : false,
		modal : true,
		buttons : [ {
			text : '保存',
			handler : function() {
				if (name == "addFolder") {
					addFolder();
				}
				if (name == "updateFolder") {
					updateFolder();
				}
				if (name == "add") {
					if ($("#path").validatebox("isValid")) {
						addMeun();
					}
				}
				if (name == "update") {
					if ($("#path").validatebox("isValid")) {
						updateMeun();
					}
				}
			}
		}, {
			text : '关闭',
			handler : function() {
				$('#dialog').window('close');
			}
		} ]
	});
}

//清除缓存
function clearCache() {
	$("#folderName,#name,#path").val("");
}

function addFolder() {
	$.ajax({
		type : "post",
		url : "/rbac/addMenu",
		data : {
			"resources.name" : $("#folderName").val(),
			"resources.sort" : $('#folderSort').numberspinner('getValue'),
			"resources.type" : "menu"
		},
		dataType : "json",
		success : function(result) {
			if (result.msg == "ok") {
				getZtreeData();
				$('#dialog').window('close');
			}
		}
	});
}

function updateFolder(id) {
	$.ajax({
		type : "post",
		url : "/rbac/updateMenu",
		data : {
			"resources.id" : tree.getSelectedNodes()[0].id,
			"resources.name" : $("#folderName").val(),
			"resources.sort" : $('#folderSort').numberspinner('getValue'),
			"resources.type" : "menu"
		},
		dataType : "json",
		success : function(result) {
			if (result.msg == "ok") {
				getZtreeData();
				$('#dialog').window('close');
			}
		}
	});
}

function delFolder() {
	$.ajax({
		type : "post",
		url : "/rbac/deleteMenu",
		data : {
			"resources.id" : tree.getSelectedNodes()[0].id
		},
		dataType : "json",
		success : function(result) {
			if (result.msg == "ok") {
				getZtreeData();
			}
		}
	});
}

function addMeun(id) {
	$.ajax({
		type : "post",
		url : "/rbac/addMenu",
		data : {
			"resources.name" : $("#name").val(),
			"resources.path" : $("#path").val(),
			"resources.pid" : tree.getSelectedNodes()[0].id,
			"resources.sort" : $('#sort').numberspinner('getValue'),
			"resources.type" : "menu"
		
		},
		dataType : "json",
		success : function(result) {
			if (result.msg == "ok") {
				getZtreeData();
				$('#dialog').window('close');
			}
		}
	});
}

function updateMeun(id) {
	$.ajax({
		type : "post",
		url : "/rbac/updateMenu",
		data : {
			"resources.id" : tree.getSelectedNodes()[0].id,
			"resources.name" : $("#name").val(),
			"resources.path" : $("#path").val(),
			"resources.pid" : tree.getSelectedNodes()[0].getParentNode().id,
			"resources.sort" : $('#sort').numberspinner('getValue'),
			"resources.type" : "menu"
		},
		dataType : "json",
		success : function(result) {
			if (result.msg == "ok") {
				getZtreeData();
				$('#dialog').window('close');
			}
		}
	});
}

function delAction() {
	$.ajax({
		type : "post",
		url : "/rbac/deleteMenu",
		data : {
			"resources.id" : tree.getSelectedNodes()[0].id
		},
		dataType : "json",
		success : function(result) {
			if (result.msg == "ok") {
				getZtreeData();
			}
		}
	});
}

window.onresize = function() {
	$("#menuPanel").panel({
		height : getWindowSize()[1] - 20
	});
};

function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if (typeof (window.innerWidth) == 'number') {
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement
			&& (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body
			&& (document.body.clientWidth || document.body.clientHeight)) {
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return ([ myWidth, myHeight ]);
}
